package com.insigma.business.qrcode.soldier.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import com.insigma.business.qrcode.common.dao.CommonAdminAreaDO;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.service.GovOrgService;
import com.insigma.business.qrcode.common.util.RedisUtils;
import com.insigma.business.qrcode.soldier.entity.*;
import com.insigma.business.qrcode.soldier.service.*;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantInfoDO;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;


@Service
public class MiniCacheUtils {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;

    @Autowired
    private CareWelfareMerchantInfoService careWelfareMerchantInfoService;

    private String PREFIX = "MiniCacheUtils";

    private Long EXPIRED_TIME = 300L;

    @Autowired
    private PlacementPositionInfoService placementPositionInfoService;

    @Autowired
    private PlacementBatchService placementBatchService;

    @Autowired
    private GovOrgService govOrgService;

    @Autowired
    private VenueAppointmentScheduleService venueAppointmentScheduleService;

    @Autowired
    private VenueAppointmentStatisticsService venueAppointmentStatisticsService;

    @Autowired
        private VenueInfoService venueInfoService;

    @Autowired
    private SoldierTpAllService soldierTpAllService;

    @Autowired
    private CareSoldierVisitedRecordService careSoldierVisitedRecordService;

    @Autowired
    private CommonAdminAreaService adminAreaService;
    @Autowired
    private CommonDataDictService commonDataDictService;

    
    public String getCachedIdCardNo(String soldierId) {
        if (Strings.isBlank(soldierId)) {
            return "";
        }

        String key = PREFIX + "_CACHED_ID_CARD_NO_" + soldierId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            SoldierBasicInfoDO bySoldierId = soldierBasicInfoService.getBySoldierId(soldierId);
            if (bySoldierId != null) {
                str = bySoldierId.getIdCardNo();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    
    public List<String> getCachedSoliderCrowds(String soldierId) {
        String key = PREFIX + "CACHED:SOLIDER:CROWDS:" + soldierId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            List<String> all = Lists.newArrayList("ALL");
            SoldierBasicInfoDO bySoldierId = soldierBasicInfoService.getBySoldierId(soldierId);
            if (bySoldierId.getBirthday() != null) {
                int bYear = DateUtils.toYear(bySoldierId.getBirthday());
                int nowYear = DateUtils.toYear(new Date());
                if ((nowYear - bYear) >= 30 & (nowYear - bYear) <= 50) {
                    all.add("ZN");
                }
            }
            str = JSON.toJSONString(all);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        List<String> list = JSONArray.parseArray(str, String.class);
        return list;
    }

    public String getCachedPlacementPositionName(String positionId) {
        if (Strings.isBlank(positionId)) {
            return "";
        }

        String key = PREFIX + "_CACHED_POSITION_ID_" + positionId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            PlacementPositionInfoDO positionInfoDO = placementPositionInfoService.get(positionId);
            if (positionInfoDO != null) {
                str = positionInfoDO.getUnitName() + "-" + positionInfoDO.getName();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public Integer getCachedPlacementPositionQuota(String positionId) {
        if (Strings.isBlank(positionId)) {
            return 0;
        }

        String key = PREFIX + "_CACHED_POSITION_ID_QUOTA" + positionId;
        Integer str = (Integer) redisUtils.get(key);
        if (str == null) {
            PlacementPositionInfoDO positionInfoDO = placementPositionInfoService.get(positionId);
            if (positionInfoDO != null) {
                str = positionInfoDO.getQuota();
            } else {
                str = 0;
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public PlacementPositionInfoDO getCachedPlacementPosition(String positionId) {
        if (Strings.isBlank(positionId)) {
            return null;
        }

        String key = PREFIX + "CACHEDPLACEMENTPOSITION" + positionId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            PlacementPositionInfoDO positionInfoDO = placementPositionInfoService.get(positionId);
            String value = JSON.toJSONString(positionInfoDO);
            str = value;
            redisUtils.set(key, value, EXPIRED_TIME);
        }
        return JSONObject.parseObject(str, PlacementPositionInfoDO.class);
    }

    public String getCachedBatchType(String batchId) {
        if (Strings.isBlank(batchId)) {
            return "";
        }

        String key = PREFIX + "GETCACHEDBATCHTYPE" + batchId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            PlacementBatchDO placementBatchDO = placementBatchService.get(batchId);
            str = placementBatchDO.getType();
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public String getCachedGovTypeOfEmployee(String employeeId) {
        if (Strings.isBlank(employeeId)) {
            return null;
        }
        String key = PREFIX + ":GOVTYPEOFEMPLOYEE:" + employeeId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            GovOrgDO govOfEmploy = govOrgService.getGovOfEmploy(employeeId);
            str = govOfEmploy.getType();
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public GovOrgDO getCachedGovOfEmployee(String employeeId) {
        if (Strings.isBlank(employeeId)) {
            return null;
        }
        String key = PREFIX + ":GOVOFEMPLOYEE:" + employeeId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            GovOrgDO govOfEmploy = govOrgService.getGovOfEmploy(employeeId);
            str = JSON.toJSONString(govOfEmploy);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSONObject.parseObject(str, GovOrgDO.class);
    }

    public String getTypeOfGov(String govId) {
        if (Strings.isBlank(govId)) {
            return null;
        }
        String key = PREFIX + ":TYPEOFGOV:" + govId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            GovOrgDO gov = govOrgService.get(govId);
            if (gov != null) {
                str = gov.getType();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public VenueAppointmentScheduleDO getCachedVenueSchedule(String id) {
        if (Strings.isBlank(id)) {
            return null;
        }
        String key = PREFIX + ":VENUESCHEDULE:" + id;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            VenueAppointmentScheduleDO scheduleDO = venueAppointmentScheduleService.get(id);
            str = JSON.toJSONString(scheduleDO);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSONObject.parseObject(str, VenueAppointmentScheduleDO.class);
    }

    public VenueAppointmentScheduleDO getCachedVenueScheduleByStatistics(String appointStatisticsId) {
        if (Strings.isBlank(appointStatisticsId)) {
            return null;
        }
        String key = PREFIX + ":VENUESCHEDULE:BYSTATISTICS" + appointStatisticsId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            VenueAppointmentStatisticsDO statisticsDO = venueAppointmentStatisticsService.get(appointStatisticsId);
            VenueAppointmentScheduleDO scheduleDO = venueAppointmentScheduleService.get(statisticsDO.getAppointTimeId());
            str = JSON.toJSONString(scheduleDO);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSONObject.parseObject(str, VenueAppointmentScheduleDO.class);

    }

    public VenueAppointmentStatisticsDO getCachedVenueStatistics(String appointStatisticsId) {
        if (Strings.isBlank(appointStatisticsId)) {
            return null;
        }
        String key = PREFIX + ":VENUESTATISTICS:" + appointStatisticsId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            VenueAppointmentStatisticsDO scheduleDO = venueAppointmentStatisticsService.get(appointStatisticsId);
            str = JSON.toJSONString(scheduleDO);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSONObject.parseObject(str, VenueAppointmentStatisticsDO.class);
    }

    public String getCachedVenueName(String venueId) {
        if (Strings.isBlank(venueId)) {
            return null;
        }
        String key = PREFIX + ":VENUENAME:" + venueId;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            VenueInfoDO venueInfoDO = venueInfoService.get(venueId);
            if (venueInfoDO != null) {
                str = venueInfoDO.getName();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public String getCachedMerchantNameById(String id) {
        if (Strings.isBlank(id)) {
            return "暂无数据";
        }
        String key = PREFIX + ":MERCHANTNAMEBYID:" + id;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            CareWelfareMerchantInfoDO merchantInfoDO = careWelfareMerchantInfoService.get(id);
            if (merchantInfoDO != null) {
                str = merchantInfoDO.getName();
            } else {
                str = "暂无数据";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public String getCachedMerchantLogById(String id) {
        if (Strings.isBlank(id)) {
            return "";
        }
        String key = PREFIX + ":GETCACHEDMERCHANTLOGBYID:" + id;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            CareWelfareMerchantInfoDO merchantInfoDO = careWelfareMerchantInfoService.get(id);
            if (merchantInfoDO != null) {
                str = merchantInfoDO.getLogo();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public Set<String> getCachedLowSideSet() {
        String key = "S:LOWSIDESET";

        String str = (String) redisUtils.get(key);
        if (str == null) {
            List<String> idCardNoList = soldierTpAllService.listIdCardNoOfLowSide();
            str = JSON.toJSONString(idCardNoList);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSON.parseObject(str, Set.class);
    }

    public Set<String> getCachedSubsistenceAllowanceSet() {
        String key = "S:SubsistenceAllowance";

        String str = (String) redisUtils.get(key);
        if (str == null) {
            List<String> idCardNoList = soldierTpAllService.listIdCardNoOfSubsistenceAllowance();
            str = JSON.toJSONString(idCardNoList);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSON.parseObject(str, Set.class);
    }

    public Set<String> getCachedExtremelyPoorSet() {
        String key = "S:ExtremelyPoor";

        String str = (String) redisUtils.get(key);
        if (str == null) {
            List<String> idCardNoList = soldierTpAllService.listIdCardNoOfExtremelyPoor();
            str = JSON.toJSONString(idCardNoList);
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return JSON.parseObject(str, Set.class);
    }

    public Boolean isVisitedThisMonth(String idCardNo, String month) {
        String key = PREFIX + "isVisitedThisMonth:" + idCardNo;

        Boolean b = (Boolean) redisUtils.get(key);
        if (b == null) {
            Integer i =  careSoldierVisitedRecordService.isVisitedOfMonth(idCardNo, month);
            if (i != null && i > 0) {
                b = true;
            } else {
                b = false;
            }
            redisUtils.set(key, b, EXPIRED_TIME);
        }
        return b;
    }

    public String getCachedAreaValue(String areaCode) {
        if (Strings.isBlank(areaCode)) {
            return "";
        }
        String key = PREFIX + ":AREAVALUE:" + areaCode;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            CommonAdminAreaDO commonAdminAreaDO = adminAreaService.get(areaCode);
            if (commonAdminAreaDO != null) {
                str = commonAdminAreaDO.getValue();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    public String getCachedSoldierType(String idCardNo) {
        if (Strings.isBlank(idCardNo)) {
            return "";
        }

        String key = PREFIX + ":SoldierType:idcard:" + idCardNo;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            SoldierBasicInfoDO bySoldierId = soldierBasicInfoService.getByIdCardNo(idCardNo);
            if (bySoldierId != null) {
                str = bySoldierId.getSoldierType();
            } else {
                str = "";
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }

    
    public String getNotSetCachedSocialSecurityStatus(String idCardNo) {
        String key = PREFIX + ":SocialSecurity:idcard:" + idCardNo;
        String str = (String) redisUtils.get(key);
        return str;
    }
    public void setCachedSocialSecurityStatus(String idCardNo, String status) {
        String key = PREFIX + ":SocialSecurity:idcard:" + idCardNo;
        redisUtils.set(key, status, EXPIRED_TIME);
    }

    public String getCachedQtGuaHaoName(String guahaoks) {
        if (Strings.isBlank(guahaoks)) {
            return "";
        }
        String key = PREFIX + ":qtGuaHaok:" + guahaoks;
        String str = (String) redisUtils.get(key);
        if (str == null) {
            CommonDataDictDO byCode = commonDataDictService.getByCode("ZYQT_" + guahaoks);
            if (byCode == null) {
                str = "";
            } else {
                str = byCode.getValue();
            }
            redisUtils.set(key, str, EXPIRED_TIME);
        }
        return str;
    }
}
